package nio.bytebuffer;

import java.nio.ByteBuffer;

public class Compact {

    public static void main(String[] args) {

        ByteBuffer byteBuffer1 = ByteBuffer.wrap(new byte[] { 1, 2, 3, 4, 5, 6 });
        System.out.println(" A capacity=" + byteBuffer1.capacity()
                + " position=" + byteBuffer1.position()
                + " limit=" + byteBuffer1.limit()); // A capacity=6 position=0 limit=6

        System.out.println(" 1 getValue=" + byteBuffer1.get()); // 1 getValue=1
        System.out.println(" B capacity=" + byteBuffer1.capacity()
                + " position=" + byteBuffer1.position()
                + " limit=" + byteBuffer1.limit()); // B capacity=6 position=1 limit=6

        System.out.println(" 2 getValue=" + byteBuffer1.get()); // 2 getValue=2
        System.out.println(" C capacity=" + byteBuffer1. capacity()
                + " position=" + byteBuffer1.position()
                + " limit=" + byteBuffer1.limit()); // C capacity=6 position=2 limit=6

        byteBuffer1.compact();

        System.out.println(" byteBuffer1. compact()");
        System.out.println(" D capacity=" + byteBuffer1.capacity()
                + " position=" + byteBuffer1.position()
                + " limit=" + byteBuffer1.limit()); // D capacity=6 position=4 limit=6

        byte[] getByteArray = byteBuffer1.array();
        for (int i = 0; i < getByteArray.length; i++) {
            System.out.print(getByteArray[i] + " ");
        }
        // 3 4 5 6 5 6
    }

}
